<?php

namespace Common\Model;

use Think\Model;

class UserOrderModel extends Model {

    protected $_auto = array(/* array(完成字段1,完成规则,[完成条件,附加规则]) */
        array('addTime', 'time', 1, 'function'),
        array('updateTime', 'time', 2, 'function'),
        array('isOn', '1', 1),
    );  // 自动完成定义

    /**
     *  获取用户一个订单
     *  @param $where
     */
    public function isOrder($userID="", $orderID="", $where = array()) {
        if(!empty($userID)) $where['userID'] = $userID;
        if(!empty($orderID)) $where['orderID'] = $orderID;
        $where['isOn'] = 1;
        $result = $this->where($where)->find();

        return $result;
    }

    /**
     *  获取用户订单列表
     *  @param $where
     */
    public function getOrderList($page = 1, $pageSize = 15, $where = array(), $order = "orderID desc") {
        $result = $this->where($where)->page($page, $pageSize)->order($order)->select();
        
        return $result;
    }

    /**
     *  获取用户地址总数
     *  @param $where
     */
    public function getOrderListLength($where = array()) {
        $result = $this->where($where)->count();

        return $result;
    }

    /**
     * 更新(注意$data一定要有主键id,若$data为空，则默认是对$_POST数组处理)
     */
    public function updateOrder($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->save();
        }
        return $rt;
    }
    
    public function getOrder($where = array()) {
        return $this->where($where)->find();
    }
    
    /**
     * 根据订单id得到该用户openid
     * @param type $orderID
     */
    public function getOneByOrderId($orderID) {
        $pre = C("DB_PREFIX");
        $arr = M("UserOrder uo")
                ->field('ur.openid openid,uo.orderSn orderSn')
                ->join("LEFT JOIN $pre"."user ur on uo.userID = ur.userID")
                ->where("uo.orderID = $orderID")
                ->find();
        return $arr;
        
    }
    
}

?>